/**
    @file       2007_V8_D.c
**/

#include <stdio.h>
#include <stdlib.h>


//#define DEBUG

#ifdef DEBUG
    #define ASSERT(cond)                                                                        \
    if (!(cond))                                                                                \
    {                                                                                           \
        printf("The state is wrong: %s, file: %s, line: %d\n", #cond, __FILE__, __LINE__);      \
        abort();                                                                                \
    }

#else
    #define ASSERT(cond)
#endif //DEBUG


//#define _EJC

#ifdef  _EJC
    #define OUT if (0)
#else
    #define OUT
#endif // _EJC

/**
    Sets_amount             finds the amount of ordered sets of digits with determined sum

    @param      sum         the sum of digits in one set
    @param      nDigits     the number of digits in one set

    @return                 the amount of sets
**/

long Sets_amount (int sum, int nDigits);


int main ()
{
    int sum = 0, nDigits = 0;

    OUT printf ("# This program finds the amount of ordered sets of digits\n"
                "  with determined sum\n"
                "# The developer:   Yura Gorishniy      <strausmg@gmail.com>\n"
                "# Version 1.00\n"
                "# The file: %s\n"
                "# The compilation time: %s, %s\n\n", strrchr (__FILE__, '\\'), __DATE__, __TIME__);

    OUT printf ("# Enter a sum and an amount of digits (type them and press ENTER)\n\n");
    if (scanf ("%d %d", &sum, &nDigits) != 2)
    {
        printf ("# Incorrect input, try again");
        return -1;
    }

    long amount = Sets_amount (sum, nDigits);
    OUT printf ("\n" "# The amount of sets:\n");
    printf ("%ld", amount);
    OUT printf ("\n");
}

long Sets_amount (int sum, int nDigits)
{
    if (nDigits == 1 && 0 <= sum && sum <= 9) return 1;
    if (sum == 0 && nDigits >= 0) return 1;
    if (sum < 0 || nDigits < 1 || (sum > nDigits * 9)) return 0;

    long amount = 0;
    int  digit = 0;

    for (digit = 0; digit <= 9; ++digit)
        amount +=  Sets_amount (sum - digit, nDigits - 1);

return amount;
}
